package com.mentaleye.biz.impl;

import com.mentaleye.bean.AutoFormBean;
import com.mentaleye.biz.AutoFormBiz;
import com.mentaleye.dal.model.AutoFormExample;
import com.mentaleye.dal.model.AutoFormWithBLOBs;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.service.AutoFormService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.ObjectUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.mentaleye.util.ObjectUtil.transferListMap;

@Service
public class AutoFormBizImpl
  implements AutoFormBiz
{
  private static Logger logger = Logger.getLogger(AutoFormBizImpl.class);

  @Autowired
  private AutoFormService autoFormService;

  public boolean addForm(AutoFormBean autoForm) {


    boolean success;
    try {
      if(autoForm.getId()!=null){
        success = addFormByLast(autoForm);
      }else{
        autoForm.setFormStatus(CommonStatus.NOMAL.getCode());
        autoForm.setCreateTime(new Date());
        success = this.autoFormService.addForm((AutoFormWithBLOBs)ObjectUtil.transfer(autoForm, AutoFormWithBLOBs.class));
      }

    }
    catch (Exception e)
    {
      success = false;
      logger.error("添加表单异常", e);
    }

    return success;
  }

  public boolean addFormByLast(AutoFormBean autoForm)
  {
    boolean success;
    try {
      AutoFormWithBLOBs bo=(AutoFormWithBLOBs)ObjectUtil.transfer(autoForm, AutoFormWithBLOBs.class);
      success = this.autoFormService.addFormByLast(bo);
    }
    catch (Exception e)
    {
      success = false;
      logger.error("更新表单异常", e);
    }
    return success;
  }

  public CommonResult disableForm(List<Integer> ids)
  {
    CommonResult commonResult=new CommonResult();
    try {
      AutoFormWithBLOBs autoForm = new AutoFormWithBLOBs();
      AutoFormExample example = new AutoFormExample();
      example.createCriteria().andIdIn(ids);
      autoForm.setFormStatus(CommonStatus.DELETE.getCode());
      commonResult.setIsSuccess(this.autoFormService.editForm(autoForm, example));
    }
    catch (Exception e)
    {
      logger.error("删除表单异常", e);
    }
    return commonResult;
  }

  public AutoFormBean getFormById(Integer id, Integer version)
  {
    AutoFormBean autoFormBean;
    try
    {
      autoFormBean = (AutoFormBean)ObjectUtil.transfer(this.autoFormService
        .getFormById(id, version), 
        AutoFormBean.class);
    }
    catch (Exception e)
    {
      autoFormBean = null;
      logger.error("获取表单异常", e);
    }

    return autoFormBean;
  }

  @Override
  public   List<AutoFormBean> queryAutoFormByMap(Map<String, Object> map) {
    try
    {
    List data = this.autoFormService
            .selectByMap(map);
     List<AutoFormBean> autoFormBeen= ObjectUtil.transferListMap(data, AutoFormBean.class);
    return  autoFormBeen;
    } catch (Exception e) {
      logger.error("查询模版异常", e);
      return  null;
    }
  }

  public void queryAutoFormByPage(CommonPage commonPage)
  {
    try
    {
      List data = this.autoFormService
        .selectByMap(commonPage
        .pageToMap());
      commonPage.setPageData(transferListMap(data, AutoFormBean.class));
    } catch (Exception e) {
      logger.error("查询课程分页异常", e);
    }
  }
}